package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkQualityNode;
import com.ysd.lis.entity.WkQualityNode;
import com.ysd.lis.mapper.WkQualityNodeMapper;
import com.ysd.lis.request.WkQualityNodeRequest;
import com.ysd.lis.service.WkQualityNodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 质量控制-质量控制节点维护
 *
 * @author LuoXS
 * @date 2024/5/15
 * version 1.0
 */
@Service
public class WkQualityNodeServiceImpl extends ServiceImpl<WkQualityNodeMapper, WkQualityNode> implements WkQualityNodeService {

    @Resource
    WkQualityNodeMapper mapper;


    @Override
    public Result pageList(WkQualityNodeRequest.PageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkQualityNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkQualityNode::getDelFlag, 0);
            if (StringUtils.isNotBlank(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(
                        w -> w.like(WkQualityNode::getCode, pageParam.getSearchValue())
                                .or()
                                .like(WkQualityNode::getName, pageParam.getSearchValue())
                );
            }
            lambdaQueryWrapper.orderByAsc(WkQualityNode::getCreateTime);
            List<WkQualityNode> wkCalcFormulas = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(wkCalcFormulas);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "查询失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result selectList(WkQualityNode param) {
        Result result;
        try {
            LambdaQueryWrapper<WkQualityNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkQualityNode::getDelFlag, 0);
            List<WkQualityNode> dictList = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(dictList);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "查询失败", e.getMessage());
        }
        return result;
    }

    @Override
    public Result add(WkQualityNode params) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            int insert = mapper.insert(params);
            if (insert < 0) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "添加失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result updateDit(WkQualityNode params) {
        boolean b = this.updateById(params);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result delete(WkQualityNode deleteParams) {
        Result result;
        try {
            int i = mapper.deleteById(deleteParams.getId());
            if (i > 0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", e.getMessage());
        }
        return result;
    }
}
